package com.cnyz.service.impl;

import com.cnyz.base.PageResponse;
import com.cnyz.dto.CourseDto;
import com.cnyz.entity.Course;
import com.cnyz.entity.StudentCourse;
import com.cnyz.entity.User;
import com.cnyz.mapper.CourseMapper;
import com.cnyz.mapper.CustomerMapper;
import com.cnyz.service.CourseService;
import com.cnyz.vo.CourseVo;
import com.cnyz.vo.UserVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseServiceImp implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Long insertCourse(CourseDto courseDto) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDto, course);
        User teacher = customerMapper.getById(courseDto.getTeacherID());
        course.setTeacherName(teacher.getUsername());
        courseMapper.insertCourse(course);
        return course.getId();
    }

    @Override
    public void updateCourse(CourseDto courseDto) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDto, course);
        User teacher = customerMapper.getById(courseDto.getTeacherID());
        course.setTeacherName(teacher.getUsername());
        courseMapper.updateCourse(course);
    }

    @Override
    public void deleteCourse(Long courseID) {
        courseMapper.deleteCourse(courseID);
    }

    @Override
    public List<CourseVo> getCourseByTeacherID(Long teacherID){
        List<CourseVo> result =courseMapper.selectCourseByTeacherID(teacherID);
        User teacher = customerMapper.getById(teacherID);
        String teacherName = teacher.getUsername();
        result.forEach(i -> i.setTeacherName(teacherName));
        return result;
    }

    @Override
    public List<CourseVo> getCourseByStudentID(Long studentID){
        List<CourseVo> result =courseMapper.selectCourseByStudentID(studentID);
        User teacher;
        for(CourseVo i : result){
            teacher = customerMapper.getById(i.getTeacherID());
            i.setTeacherName(teacher.getUsername());
        }
        return result;
    }

    @Override
    public void addStudentToCourse(Long courseID, Long studentID) {
        StudentCourse studentCourse = new StudentCourse();
        studentCourse.setCourseID(courseID);
        studentCourse.setStudentID(studentID);
        courseMapper.addStudentToCourse(studentCourse);
    }

    @Override
    public PageResponse<UserVo> getStudentByCourseID(Integer pageNum, Integer pageSize, Long courseID) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserVo> users = courseMapper.selectStudentByCourseID(courseID);
        Page<UserVo> pages = (Page<UserVo>) users;
        return PageResponse.of(pages, UserVo.class);
    } // 分页查询用户信息

    @Override
    public boolean isStudentInCourse(Long courseID, Long studentID) {
        Boolean result = courseMapper.isStudentInCourse(courseID, studentID);
        return result != null && result;
    }

    @Override
    public void removeStudentFromCourse(Long courseID, Long studentID) {
        courseMapper.removeStudentFromCourse(courseID,studentID);
    }

    @Override
    public PageResponse<UserVo> getMembersByName(Integer pageNum, Integer pageSize, Long courseID, String username) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserVo> users = courseMapper.getMembersByName(courseID, username);
        Page<UserVo> pages = (Page<UserVo>) users;
        return PageResponse.of(pages, UserVo.class);
    }
}
